//
//  ViewController.m
//  RSL10N_I18NTest
//
//  Created by thinkjoy on 2017/8/16.
//  Copyright © 2017年 杜瑞胜. All rights reserved.
//

#import "ViewController.h"

#import "NSBundle+RSUtils.h"

#import "AppDelegate.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UILabel *testLabTwo;

@property (weak, nonatomic) IBOutlet UISegmentedControl *changeLanguageSegment;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.testLabTwo.text   =   NSLocalizedStringFromTable(@"TestKey", @"RSLocalizable", @"Lab info");
    self.iconImageV.image   =   [UIImage imageNamed:NSLocalizedStringFromTable(@"APPIconKey", @"RSLocalizable", @"Icon image name")];
    
    NSString *language = [RSConfig appUseLanguage];
    
    if (language==nil) {
        self.changeLanguageSegment.selectedSegmentIndex =   3;
    }else{
        if ([language hasPrefix:@"zh-Hans"]) {
            self.changeLanguageSegment.selectedSegmentIndex =   0;
        }else if ([language hasPrefix:@"zh-Hant"]){
            self.changeLanguageSegment.selectedSegmentIndex =   2;
        }else {
            self.changeLanguageSegment.selectedSegmentIndex =   1;
        }
    }
    
}

- (IBAction)clickChangeLanguageSegment:(UISegmentedControl *)sender
{
    switch (sender.selectedSegmentIndex) {
        case 0:
        {
            NSLog(@"汉语(简)");
            [RSConfig setAppUseLanguage:@"zh-Hans"];
        }
            break;
        case 1:
        {
            NSLog(@"英文");
            [RSConfig setAppUseLanguage:@"en"];
        }
            break;
        case 2:
        {
            NSLog(@"汉语(繁)");
            [RSConfig setAppUseLanguage:@"zh-Hant"];
        }
            break;
        case 3:
        {
            NSLog(@"跟随系统");
            [RSConfig setAppUseLanguage:nil];
        }
            break;
            
        default:
            break;
    }
    
    UIViewController * VC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIApplication sharedApplication].keyWindow.rootViewController = VC;
        
        NSLog(@"已切换到语言%@",[NSBundle currentLanguage]);
    });
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
